//
//  PillowSleepDetectionViewController.m
//  Lightthings
//
//  Created by 王岩 on 2025/2/13.
//

#import "PillowSleepDetectionViewController.h"
#import "SleepChartView.h"

@interface PillowSleepDetectionViewController ()
/* <#note#> */
@property (nonatomic, strong) UILabel * statusLab;
@property (nonatomic, strong) SleepChartView * xinlvChartView;
@property (nonatomic, strong) SleepChartView * huxilvChartView;
@property (nonatomic, strong) SleepChartView * hanshengChartView;
@property (nonatomic, strong) SleepChartView * tidongChartView;
@end

@implementation PillowSleepDetectionViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.jy_title = SWString(@"睡眠监测");
    
    NSString * uuid = self.metaInfo[@"uuid"];
    NSArray * list = [FRQBluetoothManager shareManager].curPeripherals;
    for (CBPeripheral * per in list) {
        if ([per.identifier.UUIDString isEqualToString:uuid]) {
            self.peripheral = per;
        }
    }
    _statusLab = [JYInitializer jy_initLabelWithText:SWString(@"已连接设备") textColor:KLColor(@"#000000") font:U_font(14) bgColor:[UIColor whiteColor] on:self.view layout:^(MASConstraintMaker * _Nonnull make) {
        kMakeLV(15);
        kMakeTV(self.jy_bar_h);
        kMakeHV(50);
    }];
    
    
    UIScrollView * scroolView = [[UIScrollView alloc] initWithFrame:CGRectMake(15, self.jy_bar_h + 50, kScreenWidth - 30, kScreenHeight - self.jy_bar_h- klSafeAreaEdgeInsets.bottom - 50)];
    scroolView.contentSize = CGSizeMake(kScreenWidth - 30, 1280);
    [self.view addSubview:scroolView];
    
    _xinlvChartView = [self cChartViewWithT:SWString(@"心率") Frame:CGRectMake(0, 0, kScreenWidth - 30, 300) lineColor:@"#E44614" Icon:@"心率"];
    [scroolView addSubview:_xinlvChartView];
    
    _huxilvChartView = [self cChartViewWithT:SWString(@"呼吸率") Frame:CGRectMake(0, 320, kScreenWidth - 30, 300) lineColor:@"#E44614" Icon:@"呼吸率"];
    [scroolView addSubview:_huxilvChartView];
    
    _hanshengChartView = [self cChartViewWithT:SWString(@"鼾声") Frame:CGRectMake(0, 640, kScreenWidth - 30, 300) lineColor:@"#E44614" Icon:@"打鼾 1"];
    [scroolView addSubview:_hanshengChartView];
    
     _tidongChartView = [self cChartViewWithT:SWString(@"体动") Frame:CGRectMake(0, 960, kScreenWidth - 30, 300) lineColor:@"#E44614" Icon:@"体动"];
    [scroolView addSubview:_tidongChartView];
    
    [self readData];
    // Do any additional setup after loading the view.
}

- (SleepChartView *)cChartViewWithT:(NSString *)title  Frame:(CGRect)frame lineColor:(NSString *)color Icon:(NSString *)icon {
    SleepChartView * view = [[ SleepChartView alloc] initWithFrame:frame];
    view.title = title;
    view.value = @"0";
    view.icon = icon;
    view.lineColor = color;
    return view;
}

- (void)readData {
    [PillowCommandTool.sharedInstance updateDateAndTimeCallBack:^(NSString * data) {
        // feef b0 41 00 00 00 00 00 3232323232323232323232323232323232323232 3d3d3d3d3d3d3c3c3c3c3b3b3b3a3a3a39393838 0000000000000000000000000000000000000000 e844
        if ([[data substringWithRange:NSMakeRange(4, 2)] isEqualToString:@"b0"]) {
            
            NSString * sleepdata = [data substringFromIndex:8];
            sleepdata  = [sleepdata substringToIndex:sleepdata.length - 4];
            
            NSString * heart = [sleepdata substringWithRange:NSMakeRange(0, 2)];
            long heartValue = strtoll([[NSString stringWithFormat:@"0x%@",heart] UTF8String], 0, 16);
            [self.xinlvChartView updateLine:heart];
            NSString * huxilv = [sleepdata substringWithRange:NSMakeRange(2, 2)];
            long huxilvValue = strtoll([[NSString stringWithFormat:@"0x%@",huxilv] UTF8String], 0, 16);
            [self.huxilvChartView updateLine:huxilv];
            NSString * dahan = [sleepdata substringWithRange:NSMakeRange(4, 2)];
            long dahanValue = strtoll([[NSString stringWithFormat:@"0x%@",dahan] UTF8String], 0, 16);
            [self.hanshengChartView updateLine:dahan];
            NSString * tidong = [sleepdata substringWithRange:NSMakeRange(6, 2)];
            long tidongValue = strtoll([[NSString stringWithFormat:@"0x%@",tidong] UTF8String], 0, 16);
            [self.tidongChartView updateLine:tidong];
            NSString * status = [sleepdata substringWithRange:NSMakeRange(8, 2)];
            long statusValue = strtoll([[NSString stringWithFormat:@"0x%@",status] UTF8String], 0, 16);
            
            NSString * xinlvChart = [sleepdata substringWithRange:NSMakeRange(10, 40)];
            
            if (huxilvValue > 0 && heartValue > 0) {
                self.statusLab.text = SWString(@"已连接设备 - 在床");
            }else {
                self.statusLab.text = SWString(@"已连接设备 - 离床");
            }
        }
    } RealTimeData:YES StopSwith:NO ToPeripheral:self.peripheral];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
